import '../../../../Include.dart';

class DemoFlowMenuPage extends BasePage {
  static String dartPath = "lib/pages/component/single/flow/DemoFlowMenuPage.dart";
  @override
  _DemoFlowMenuPageState createState() => _DemoFlowMenuPageState();

  @override
  String get pageName => 'DemoFlowMenuPage【半圆菜单展开/收起】';
}

class _DemoFlowMenuPageState extends BasePageState<DemoFlowMenuPage>  with TickerProviderStateMixin {
  //动画需要这个类来混合
  //动画变量,以及初始化和销毁
  AnimationController _ctrlAnimationCircle;


  @override
  void initState() {
    super.initState();

    _ctrlAnimationCircle = AnimationController(
      //初始化动画变量
        lowerBound: 0,
        upperBound: 80,
        duration: Duration(milliseconds: 300),
        vsync: this);
    _ctrlAnimationCircle.addListener(() => setState(() {}));

  }

  @override
  void dispose() {
    _ctrlAnimationCircle.dispose(); //销毁变量,释放资源
    super.dispose();
  }

  //生成Flow的数据
  List<Widget> _buildFlowChildren() {
    return List.generate(
        5,
            (index) => Container(
          child: Icon(
            index.isEven ? Icons.timer : Icons.ac_unit,
            color: Colors.primaries[index % Colors.primaries.length],
          ),
        ));
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.pageName),
      ),
      body: Stack(
        children: <Widget>[
          Positioned.fill(
            child: Flow(
              delegate: FlowAnimatedCircle(_ctrlAnimationCircle.value),
              children: _buildFlowChildren(),
            ),
          ),
          Positioned.fill(
            child: IconButton(
              icon: Icon(Icons.menu),
              onPressed: () {
                setState(() {
                  //点击后让动画可前行或回退
                  _ctrlAnimationCircle.status == AnimationStatus.completed
                      ? _ctrlAnimationCircle.reverse()
                      : _ctrlAnimationCircle.forward();
                });
              },
            ),
          ),
        ],
      ),
    );
  }

}

class FlowAnimatedCircle extends FlowDelegate {
  final double radius; //绑定半径,让圆动起来
  FlowAnimatedCircle(this.radius);

  @override
  void paintChildren(FlowPaintingContext context) {
    if (radius == 0) {
      return;
    }
    double x = 0; //开始(0,0)在父组件的中心
    double y = 0;
    for (int i = 0; i < context.childCount; i++) {
      x = radius * cos(i * pi / (context.childCount - 1)); //根据数学得出坐标
      y = radius * sin(i * pi / (context.childCount - 1)); //根据数学得出坐标
      context.paintChild(i, transform: Matrix4.translationValues(x, -y, 0));
    } //使用Matrix定位每个子组件
  }

  @override
  bool shouldRepaint(FlowDelegate oldDelegate) => true;
}